(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('HomeController', HomeController);

    HomeController.$inject = ['$scope','PubCardRecomments','PubCardEssences', 'Principal', 'LoginService', '$state'];

    function HomeController ($scope, PubCardRecomments,PubCardEssences,Principal, LoginService, $state) {
        var vm = this;

        vm.account = null;
        vm.isAuthenticated = null;
        vm.login = LoginService.open;
        vm.register = register;
        vm.recommends={};
        vm.essences={};
        $scope.$on('authenticationSuccess', function() {
            getAccount();
        });

        getAccount();

        function getAccount() {
            Principal.identity().then(function(account) {
                vm.account = account;
                vm.isAuthenticated = Principal.isAuthenticated;
            });
        }
        function register () {
            $state.go('register');
        }
        function loadRecommends(){
        	PubCardRecomments.query({
                count:60
            },onSuccess, onError);
            
            function onSuccess(data) {
                vm.recommends=data;
            }
            function onError(error) {
               // AlertService.error(error.data.message);
            }
        }
        function loadEssences(){
        	PubCardEssences.query({
        		count:60
        	},onSuccess, onError);
        	function onSuccess(data) {
        		vm.essences=data;
        	}
        	function onError(error) {
        		// AlertService.error(error.data.message);
        	}
        }
        loadRecommends();
        loadEssences();
    }
})();
